<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <c:import url="../../header.jsp"/>
</head>
<body>
<div class="content-warp">
    <div class="dgg-list-top-warp">
        <div class="screen-search">
            <ul>
                <li>
                    <label>时间：</label>
                    <input type="text" readonly class="dggui-input dggui-time-range" value="${curDate}" id="royaltyTime" name="royaltyTime">
                    <i class="iconfont icon-rili"></i>
                </li>
                <li>
                    <label>生产单编号：</label>
                    <input type="text" class="dggui-input dggui-input-normal" name="productOrderNo" id="productOrderNo"
                           placeholder="生产单编号">
                </li>
                <li>
                    <label>业务类型：</label>
                    <input type="hidden" name="typeCodeHidden" id="typeCodeHidden">
                    <div name="typeCode" id="typeCode"></div>
                </li>
                <li>
                    <label>产品：</label>
                    <select name="productCode" id="productCode" style="min-width: 80px; display: none;">
                    </select>
                </li>
                <li>
                    <label>渠道：</label>
                    <select name="channelCode" id="channelCode" class="dggui-select">
                        <option value=''>请选择</option>
                    </select>
                </li>
                <li>
                    <button class="dggui-btn searchBtn" onclick="tableReload();getTotalRoyaltyMy();">搜索</button>
                    <button class="dggui-btn dggui-btn-primary" onclick="cleanSearch();">清空条件</button>
                    <button class="dggui-btn dggui-btn-green"
                            onclick="excelExportMyRoyalty('${basePath}/rzsc/royalty_management/excel_my_royalty.do');">导出
                    </button>
                </li>
            </ul>
        </div>
    </div>
    <div class="list-content">
        <div class="tool-bar">
            <div class="fl">
                <span>总业绩：</span><span id="achievement"></span>&nbsp;&nbsp;&nbsp;&nbsp;
                <span>提成总金额：</span><span id="performance"></span>
            </div>
        </div>
        <div class="table-warp">
            <table class="dggui-table" id="royaltyList">
            </table>
        </div>
    </div>
</div>

<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script>
    var tables;
    var select2;
    var tableName = "royaltyList";
    dggui.use(['table','select2', 'distpicker','dggdate'], function () {
        var dggdate = dggui.dggdate;
        var distpicker = dggui.distpicker;
        select2 = dggui.select2;

        //初始化时间
        dggdate.render({
            elem: '#royaltyTime',
            type: 'date',
            range: '~'
        });
        select2.render({
            placeholder: "请选择",
            allowClear: true,
            elem: $('#productCode'),
            width: "160px"
        });
        select2.render({
            placeholder: "请选择",
            allowClear: true,
            elem: $('#channelCode'),
            width: "160px"
        });
        //初始化业务类型及产品
        distpicker.render({
            url: "${basePath}/rzsc/common/tree_book_list.do?code=BUS_YT_DK&type=2&level=2", // 请求地址
            elem: '#typeCode', // 容器Id
            type: 2,
            placeholder: '选择业态',
            defaultParentCode: 'BUS_YT_DK',
            callback: function () {//如果不需要选产品就把callback整个方法注释掉
                var code = $("#typeCode .plug-in-code").val();
                initProductCode("#productCode", code);
                select2.render({
                    placeholder: "请选择",
                    allowClear: true,
                    elem: $('#productCode'),
                    width: "160px"
                });
                var produtId = $("#productCode").val();
                initChannel("#channelCode", produtId);
                select2.render({
                    placeholder: "请选择",
                    allowClear: true,
                    elem: $('#channelCode'),
                    width: "160px"
                });
            },
            callbackClear: function () {
                $("#productCode").find("option").remove();
                $("#productCode").append("<option value=''>请选择</option>");
                $("#channelCode").find("option").remove();
                $("#channelCode").append("<option value=''>请选择</option>");
            }
        });
        //渠道初始化
        $('#productCode').on('change', function (e) {
            var produtId = $("#productCode").val();
            initChannel("#channelCode", produtId);
            select2.render({
                placeholder: "请选择",
                allowClear: true,
                elem: $('#channelCode'),
                width: "160px"
            });
        });
        //加载业绩及提成总金额数据
        getTotalRoyaltyMy();
        var table = dggui.table;
        var opt = {
            "elem": "#royaltyList",
            "ajax": sysInfo.basePath + "/rzsc/royalty_management/royalty_list.do",
            "scrollX": "true",//是否开启横项滚动
            "oneSelectCheck": true,//单选，同时选中复选框
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 1,//左侧锁定2列
                "rightColumns": 1//右侧锁定1列
            },
            "visible": true,
            "fnServerParams": function (aoData) {
                //搜索条件
                aoData = $.extend(aoData, {
                    smallBusiness: $("#typeCode .plug-in-code").val(),
                    productId: $("#productCode").val(),
                    channelId: $("#channelCode").val(),
                    royaltyTime:$("#royaltyTime").val(),
                    productOrderNo: $("#productOrderNo").val()
                    }
                )
                return aoData;
            },
            "aoColumns": [
                {
                    "title": "流程人员",
                    "data": "payUserName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "流水号",
                    "data": "payNo",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "认款时间",
                    "data": "amountMonthStr",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "生产事业部",
                    "data": "productOrderOrgName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "部门",
                    "data": "productOrderUserOrgName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "生产单编号",
                    "data": "productOrderNo",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "产品",
                    "data": "productName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "商品",
                    "data": "commodityName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }
                ,{
                    'title': '渠道',
                    "data": "channelName",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data : '';
                    }
                } ,{
                    'title': '结项内部成本（元）',
                    "data": "calculationItem.BUS_YT_DK_nbcb",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data.toFixed(2) : 0;
                    }
                }
                ,{
                    'title': '自开发利润金额（元）',
                    "data": "calculationItem.BUS_YT_DK_zkflr",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data.toFixed(2) : 0;
                    }
                } ,{
                    'title': '返点利润金额（元）',
                    "data": "calculationItem.BUS_YT_DK_fdlr",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data.toFixed(2) : 0;
                    }
                } ,{
                    'title': '业绩提成比例',
                    "data": "calculationItem.BUS_YT_DK_yjtcxs",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data : 0;
                    }
                } ,{
                    'title': '自开发提成比例',
                    "data": "calculationItem.BUS_YT_DK_zkftcxs",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data ? data : 0;
                    }
                } ,{
                    'title': '提成金额（元）',
                    "data": "performanceData",
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        return data? (Math.round(data * 100) / 100).toFixed(2): 0;
                    }
                }

            ]
        };
        tables = table.render(opt);
        // 处理dataTable 异常错误信息
        $.fn.dataTable.ext.errMode = 'none';//不抛出异常
        tables.on('error.dt', function (e, settings, techNote, message) {
            //dggui.dialog.errors("表格查询错误，请联系管理员", 2000);
            if (checkEmpty(settings.jqXHR.responseJSON.error)) {
                dggui.dialog.errors(settings.jqXHR.responseJSON.error, 2000);
            }
        });
    });

    //列表刷新
    function tableReload() {
        tables.ajax.reload();
    }

    // 清空搜索
    function cleanSearch() {
        var $s = $('.screen-search');
        $s.find('input').val('');
        $s.find('select').val('').trigger("change");
        var productCode=$("#productCode");
        var channelCode=$("#channelCode");
        if(!isEmpty(productCode)){
            $("#productCode").find("option").remove();
            $("#productCode").append("<option value=''>请选择</option>");
        }
        if(!isEmpty(channelCode)){
            $("#channelCode").find("option").remove();
            $("#channelCode").append("<option value=''>请选择</option>");
        }
    }
    /**
     * 导出
     * */
    function excelExportMyRoyalty(url) {
        var obj = new Object();
        obj.smallBusiness = $("#typeCode .plug-in-code").val();
        obj.productId = $("#productCode").val();
        obj.channelId = $("#channelCode").val();
        obj.royaltyTime = $("#royaltyTime").val();
        obj.productOrderNo =  $("#productOrderNo").val()
        formSubmit(url, obj);
    }

    /**
     * 获取总金额及业绩数据
     */
    function getTotalRoyaltyMy() {
        var royaltyTime = $("#royaltyTime").val();
        $.ajax({
            url: sysInfo.basePath + "/rzsc/royalty_management/get_total_royalty_my.do",
            data: {royaltyTime: royaltyTime},
            type: "POST",
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if(isEmpty(data)){
                        $("#performance").text("0 元");
                        $("#achievement").text("0 元");
                    }else {
                        $("#performance").text(data.performance.toFixed(2)+" 元");
                        $("#achievement").text(data.achievement.toFixed(2)+" 元");
                    }
                } else {
                    dggui.dialog.errors("获取总业绩失败");
                }
            }
        });
    }
</script>
</body>
</html>